﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class usercontrol_ViewCart : System.Web.UI.UserControl
{
    Product product = new Product();
    DaiCuongMobile.SelectedProductDataTable selectedProductTable = new DaiCuongMobile.SelectedProductDataTable();
    DaiCuongMobile.SelectedProductRow selectedProductRow;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"] != null)
        {
            selectedProductTable = (DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"];
        }

        GridVShoppingCart.DataSource = selectedProductTable;
        GridVShoppingCart.DataBind();

        // Tính tổng tiền
        if (GridVShoppingCart.Rows.Count > 0)
        {
            imgbtnOrder.Visible = true; // nút thanh toán
            imgbtnUpdateQtty.Visible = true; // nút cập nhật
            int rowIndex = GridVShoppingCart.Rows.Count;
            double Total = 0;
            for (int i = 0; i < rowIndex; i++)
            {
                Total += Convert.ToDouble(GridVShoppingCart.Rows[i].Cells[5].Text);
            }
            lblTotal.Text = Total.ToString("#0,0");
            double shipment = 0; // phí vận chuyển.
            lblShipment.Text = shipment.ToString("#0,0");
            lblGTotal.Text = Convert.ToDouble((Total + shipment)).ToString("#0,0");

        }
        else
        {
            imgbtnOrder.Visible = false;
            imgbtnUpdateQtty.Visible = false;
            lblTitle.Text = "Bạn Chưa Mua Sản Phẩm Nào!";
            double Total = 0;
            lblTotal.Text = Total.ToString("#0,0");
            double shipment = 0; // chi phí vận chuyển.
            lblShipment.Text = shipment.ToString("#0,0");
            lblGTotal.Text = Convert.ToDouble((Total + shipment)).ToString("#0,0");
        }

        Session["selectedProduct"] = selectedProductTable;  
        
    }
    protected void GridVShoppingCart_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Huy") // Xử lý bỏ sản phẩm ra khỏi giỏ hàng.
            {
                selectedProductTable = (DaiCuongMobile.SelectedProductDataTable)Session["selectedProduct"];
                int index = Convert.ToInt32(e.CommandArgument);
                selectedProductTable.Rows[index].Delete();
                GridVShoppingCart.DataSource = selectedProductTable;
                GridVShoppingCart.DataBind();

                // Tính lại tổng số tiền. 
                if (GridVShoppingCart.Rows.Count > 0)
                {
                    imgbtnOrder.Visible = true; // nút thanh toán.
                    imgbtnUpdateQtty.Visible = true; // nút cập nhật.
                    int rowIndex = GridVShoppingCart.Rows.Count;
                    double Total = 0;
                    for (int i = 0; i < rowIndex; i++)
                    {
                        //Total += Convert.ToDouble(GridVShoppingCart.Rows[i].Cells[3].Text) * Convert.ToInt32(((TextBox)GridVShoppingCart.Rows[i].Cells[4].FindControl("txtQuantity")).Text);
                        Total += Convert.ToDouble(GridVShoppingCart.Rows[i].Cells[5].Text);

                    }
                    lblTotal.Text = Total.ToString("#0,0");
                    double shipment = 0; // phí vận chuyển.
                    lblShipment.Text = shipment.ToString("#0,0");
                    lblGTotal.Text = Convert.ToDouble((Total + shipment)).ToString("#0,0");

                }
                else
                {
                    imgbtnOrder.Visible = false;
                    imgbtnUpdateQtty.Visible = false;
                    lblTitle.Text = "Bạn Chưa Mua Sản Phẩm Nào!";
                    double Total = 0;
                    lblTotal.Text = Total.ToString("#0,0");
                    double shipment = 0; // phí vận chuyển.
                    lblShipment.Text = shipment.ToString("#0,0");
                    lblGTotal.Text = Convert.ToDouble((Total + shipment)).ToString("#0,0");
                }
            }

            Session["selectedProduct"] = selectedProductTable;
        }
        catch (Exception)
        { }      
    }
    protected void imgbtnUpdateQtty_Click(object sender, ImageClickEventArgs e)
    {
        // Xử lý cập nhật giỏ hàng

        selectedProductTable.Clear();
        Session["selectedProduct"] = selectedProductTable;
        for (int i = 0; i <= GridVShoppingCart.Rows.Count - 1; i++)
        {

            selectedProductRow = selectedProductTable.NewSelectedProductRow();
            selectedProductRow["Index"] = GridVShoppingCart.Rows[i].Cells[0].Text;
            selectedProductRow["ProductID"] = GridVShoppingCart.Rows[i].Cells[1].Text;
            HyperLink productname = (HyperLink)GridVShoppingCart.Rows[i].FindControl("hplProductName");
            selectedProductRow["ProductName"] = productname.Text;
            selectedProductRow["SalePrice"] = GridVShoppingCart.Rows[i].Cells[3].Text;
            TextBox quantity = (TextBox)GridVShoppingCart.Rows[i].FindControl("txtQuantity");
            selectedProductRow["Quantity"] = Int32.Parse(quantity.Text);
            selectedProductRow["Discount"] = double.Parse(GridVShoppingCart.Rows[i].Cells[5].Text);
            //selectedProductRow["Amount"] = (double.Parse(GridVShoppingCart.Rows[i].Cells[3].Text) * Int32.Parse(quantity.Text)) - (double.Parse(GridVShoppingCart.Rows[i].Cells[3].Text) * double.Parse(GridVShoppingCart.Rows[i].Cells[5].Text));
            selectedProductRow["Amount"] = (double.Parse(GridVShoppingCart.Rows[i].Cells[3].Text) * Int32.Parse(quantity.Text));

            selectedProductTable.AddSelectedProductRow(selectedProductRow);
        }

        Session["selectedProduct"] = selectedProductTable;
        GridVShoppingCart.DataBind();

        // Tính tống số tiền
        if (GridVShoppingCart.Rows.Count > 0)
        {
            imgbtnOrder.Visible = true;
            imgbtnUpdateQtty.Visible = true;
            int rowIndex = GridVShoppingCart.Rows.Count;
            double Total = 0;
            for (int i = 0; i < rowIndex; i++)
            {
                Total += Convert.ToDouble(GridVShoppingCart.Rows[i].Cells[5].Text);
            }
            lblTotal.Text = Total.ToString("#0,0");
            double shipment = 0; // phí vận chuyển.
            lblShipment.Text = shipment.ToString("#0,0");
            lblGTotal.Text = Convert.ToDouble((Total + shipment)).ToString("#0,0");
        }
        else
        {
            imgbtnOrder.Visible = false;
            imgbtnUpdateQtty.Visible = false;
            lblTitle.Text = "Bạn Chưa Mua Sản Phẩm Nào!";
            double Total = 0;
            lblTotal.Text = Total.ToString("#0,0");
            double shipment = 0; // phí vận chuyển.
            lblShipment.Text = shipment.ToString("#0,0");
            lblGTotal.Text = Convert.ToDouble((Total + shipment)).ToString("#0,0");
        }      
    }
}
